package com.siyu.tickets.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.siyu.common.hibernate3.Finder;
import com.siyu.common.hibernate3.HibernateBaseDao;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.ElementDao;
import com.siyu.tickets.entity.Element;

@Repository
public class ElementDaoImpl extends HibernateBaseDao<Element, Integer> implements ElementDao {

	public Pagination getPage(String name, String type, Boolean enable, int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from Element bean");
		f.append(" where 1=1");
		if (!StringUtils.isBlank(name)) {
			f.append(" and ( bean.name like :name or  bean.enname like :name)");
			f.setParam("name", "%" + name + "%");
		}
		if (!StringUtils.isBlank(type)) {
			f.append(" and bean.type=:type");
			f.setParam("type", type);
		}
		if (enable != null) {
			f.append(" and bean.enable=:enable");
			f.setParam("enable", enable);
		}
		f.append(" order by bean.order asc");
		return find(f, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	public List<Element> findListByType(String type) {
		Finder f = Finder.create("select bean from Element bean");
		f.append(" where bean.enable=:enable ");
		f.setParam("enable", true);
		if (type != null) {
			f.append(" and bean.type=:type");
			f.setParam("type", type);
		}
		f.append(" order by bean.order ");
		return find(f);
	}

	public Element findById(Integer id) {
		Element entity = get(id);
		return entity;
	}

	public Element save(Element bean) {
		getSession().save(bean);
		return bean;
	}

	public Element deleteById(Integer id) {
		Element entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	protected Class<Element> getEntityClass() {
		return Element.class;
	}

}
